package busterminalweb.action.bus;

import busterminal.entity.Bus;
import busterminal.entity.BusImplementation;
import busterminal.entity.BusModel;
import busterminal.entity.BusModelImplementation;
import busterminal.entity.service.PurposeType;
import busterminal.exception.BusinessException;
import busterminal.persistence.BusDAO;
import busterminal.services.ServiceLocator;
import busterminalweb.action.Action;
import busterminalweb.action.ActionResult;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Сохранить автобус
 */
public class SaveBusAction implements Action {

    /**
     * Выполнить действие
     * @param request запрос
     * @param response ответ
     * @return страница, которой нужно передать управление
     */
    @Override
    public ActionResult execute(HttpServletRequest request, HttpServletResponse response) {
        // if Save button have been pressed
        if (request.getParameter("saveButton") != null) {
            BusDAO busDAO = ServiceLocator.getService(BusDAO.class);
            int id = Integer.parseInt(request.getParameter("id"));
            Bus bus = (id == 0) ? new BusImplementation() : busDAO.read(id);

            // load data from request
            if (request.getParameter("stateNumber") != null && !request.getParameter("stateNumber").isEmpty()) {
                bus.setStateNumber(request.getParameter("stateNumber"));
            } else {
                throw new BusinessException("State number should be not empty");
            }
            if (request.getParameter("modelName") != null && !request.getParameter("modelName").isEmpty()) {
                BusModel busModel = new BusModelImplementation();
                busModel.setId(Integer.parseInt(request.getParameter("modelName")));
                bus.setBusModel(busModel);
            } else {
                throw new BusinessException("It is necessary to define model name");
            }
            if (request.getParameter("startYearOfExploitation") != null && !request.getParameter("startYearOfExploitation").isEmpty()) {
                bus.setStartYearOfExploitation(Integer.parseInt(request.getParameter("startYearOfExploitation")));
            } else {
                throw new BusinessException("Start year of exploitation should be not empty");
            }
            if (request.getParameter("purposeType") != null && !request.getParameter("purposeType").isEmpty()) {
                bus.setPurposeType(PurposeType.valueOf(request.getParameter("purposeType")));
            } else {
                throw new BusinessException("It is necessary to define purpose type");
            }

            if (id != 0) {
                busDAO.update(bus);
            } else {
                busDAO.create(bus);
            }
        }

        request.getSession().getServletContext().setAttribute("link", "bus.list");
        
        return ActionResult.redirect("go.to");
    }

    /**
     * Получить имя действия
     * @return имя действия
     */
    @Override
    public String getName() {
        return "bus.save";
    }
}
